package com.example.electronicmall.application;

import android.app.Application;
import android.util.Log;

import com.example.electronicmall.http.ElectronicMallRequest;
import com.example.electronicmall.http.MineOrderRequest;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

import okhttp3.HttpUrl;
import okhttp3.Interceptor;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.ResponseBody;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

public class BaseApplication extends Application {

    public static ElectronicMallRequest request;

    public static MineOrderRequest mineOrderRequest;
    @Override
    public void onCreate() {
        super.onCreate();

        Retrofit retrofit=new Retrofit.Builder()
                .baseUrl("http://120.48.12.74:8080")
                .addConverterFactory(GsonConverterFactory.create())
                .client(new OkHttpClient.Builder()
                        .callTimeout(5, TimeUnit.SECONDS)
                        .readTimeout(5,TimeUnit.SECONDS)
                        .writeTimeout(5,TimeUnit.SECONDS)
                        .connectTimeout(5,TimeUnit.SECONDS)
                        .addInterceptor(new Interceptor() {
                            @Override
                            public Response intercept(Chain chain) throws IOException {
                                Request request =chain.request();
                                HttpUrl url=request.url();

                                Response response=chain.proceed(request);

                                int httpCode=response.code();
                                String httpMag=response.message();
                                String result =response.body().string();
                                MediaType type=response.body().contentType();

                                Log.d("HTTP", "URL:"+url.toString());
                                Log.d("HTTP", "State"+httpCode+""+httpMag);
                                Log.d("HTTP", "result"+result);
                                return response.newBuilder()
                                        .body(ResponseBody.create(type,result))
                                        .build();
                            }
                        }).build() )

                .build();

        request=retrofit.create(ElectronicMallRequest.class);

        //订单
        Retrofit retrofit1 = new Retrofit.Builder()
                .baseUrl("http://120.48.12.74:8080")
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .client(new OkHttpClient.Builder()
                        .callTimeout(5, TimeUnit.SECONDS)
                        //写时长
                        .writeTimeout(5, TimeUnit.SECONDS)
                        //读取时长
                        .readTimeout(5, TimeUnit.SECONDS)
                        //连接时长
                        .connectTimeout(5, TimeUnit.SECONDS)
                        //拦截器
                        .addInterceptor(new Interceptor() {
                            @Override
                            public Response intercept(Chain chain) throws IOException {
                                //拦截request
                                Request request = chain.request();
                                HttpUrl url = request.url();

                                //拦截response
                                Response response = chain.proceed(request);
                                //状态码
                                int httpCode = response.code();
                                //返回消息
                                String msg = response.message();
                                //返回结果
                                String result = response.body().string();
                                //返回类型
                                MediaType type = response.body().contentType();

                                Log.d("TAG", "请求地址: " + url.toString());
                                //Log.d("TAG", "状态码: " + httpCode + "" + msg);
                                //Log.d("TAG", "结果: " + result);
                                return response.newBuilder()
                                        .body(ResponseBody.create(type, result))
                                        .build();
                            }
                        }).build())
                .build();

        mineOrderRequest = retrofit1.create(MineOrderRequest.class);
    }

}
